home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 27 / MacFormat n. 27 (Spain) / Mac Format 26.bin / Shareware / Programación / ModDateFixer 1.0 / Source / ModDateFixer.cpp < prev   
Encoding:
C/C++ Source or Header  |  1997-02-15  |  4.2 KB  |  254 lines

  1. //    ModDateFixer.cpp
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <AppleEvents.h>
  6.  
  7.  
  8. static volatile Boolean running = true;
  9. static WindowPtr statusWindow = NULL;
  10. static unsigned long curModDate = 0;
  11.  
  12. #define Error(e) _Error(e,__FILE__,__LINE__)
  13. static void
  14. _Error(
  15.     OSErr err,
  16.     const char *file,
  17.     int line)
  18. {
  19.     char msg[200];
  20.     sprintf(msg, "error %d file %s line %d", err, file, line);
  21.     SetPort(statusWindow);
  22.     Rect r;
  23.     SetRect(&r, 0,0, 500,18);
  24.     EraseRect(&r);
  25.     MoveTo(5,15);
  26.     TextFace(bold);
  27.     DrawText(msg, 0, strlen(msg));
  28.     TextFace(normal);
  29.     long endtime;
  30.     Delay(200, &endtime);
  31. }
  32.  
  33.  
  34. static void
  35. InitToolbox()
  36. {
  37.     InitGraf(&qd.thePort);
  38.     InitFonts();
  39.     InitWindows();
  40.     InitMenus();
  41.     TEInit();
  42.     InitDialogs(NULL);
  43.  
  44.     InitCursor();
  45.     statusWindow = GetNewCWindow(128, NULL, NULL);
  46.     ShowWindow(statusWindow);
  47. }
  48.  
  49.  
  50. static void
  51. Report(
  52.     FSSpec *ptr,
  53.     int level)
  54. {
  55.     Rect r;
  56.     SetRect(&r, 5,18+15*level, 500,1000);
  57.     EraseRect(&r);
  58.     MoveTo(5+15*level, 30+15*level);
  59.     DrawString(ptr->name);
  60. }
  61.  
  62.  
  63. static void
  64. FixModDate(
  65.     FSSpec *ptr)
  66. {
  67.     CInfoPBRec rec;
  68.     rec.hFileInfo.ioFDirIndex = 0;
  69.     rec.hFileInfo.ioVRefNum = ptr->vRefNum;
  70.     rec.hFileInfo.ioDirID = ptr->parID;
  71.     rec.hFileInfo.ioNamePtr = ptr->name;
  72.     OSErr err = PBGetCatInfoSync(&rec);
  73.     if (err)
  74.     {
  75.         Error(err);
  76.     }
  77.     else
  78.     {
  79.         Boolean doIt = false;
  80.         if (rec.hFileInfo.ioFlMdDat > curModDate)
  81.         {
  82.             rec.hFileInfo.ioFlMdDat = curModDate;
  83.             doIt = true;
  84.         }
  85.         if (rec.hFileInfo.ioFlCrDat > rec.hFileInfo.ioFlMdDat)
  86.         {
  87.             rec.hFileInfo.ioFlCrDat = rec.hFileInfo.ioFlMdDat;
  88.             doIt = true;
  89.         }
  90.         if (doIt)
  91.         {
  92.             rec.hFileInfo.ioDirID = ptr->parID;
  93.             rec.hFileInfo.ioFDirIndex = 0;
  94.             err = PBSetCatInfoSync(&rec);
  95.             if (err)
  96.             {
  97.                 Error(err);
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103.  
  104. static Boolean
  105. IsFolder(
  106.     FSSpec *spec)
  107. {
  108.     CInfoPBRec rec;
  109.     rec.hFileInfo.ioFDirIndex = 0;
  110.     rec.hFileInfo.ioVRefNum = spec->vRefNum;
  111.     rec.hFileInfo.ioDirID = spec->parID;
  112.     rec.hFileInfo.ioNamePtr = spec->name;
  113.     OSErr err = PBGetCatInfoSync(&rec);
  114.     if (err)
  115.     {
  116.         Error(err);
  117.         return false;
  118.     }
  119.     return (rec.hFileInfo.ioFlAttrib&0x10) != 0;
  120. }
  121.  
  122.  
  123. static Boolean
  124. GetNthItem(
  125.     FSSpec *spec,
  126.     int ix,
  127.     FSSpec *out)
  128. {
  129.     CInfoPBRec rec;
  130.     rec.hFileInfo.ioFDirIndex = 0;
  131.     rec.hFileInfo.ioVRefNum = spec->vRefNum;
  132.     rec.hFileInfo.ioDirID = spec->parID;
  133.     rec.hFileInfo.ioNamePtr = spec->name;
  134.     OSErr err = PBGetCatInfoSync(&rec);
  135.     if (err)
  136.     {
  137.         Error(err);
  138.         return false;
  139.     }
  140.     if (!(rec.hFileInfo.ioFlAttrib&0x10))
  141.     {
  142.         return false;
  143.     }
  144.     long dirID = rec.hFileInfo.ioDirID;
  145.     rec.hFileInfo.ioFDirIndex = ix;
  146.     rec.hFileInfo.ioVRefNum = spec->vRefNum;
  147.     rec.hFileInfo.ioDirID = dirID;
  148.     rec.hFileInfo.ioNamePtr = out->name;
  149.     err = PBGetCatInfoSync(&rec);
  150.     if (err)
  151.     {
  152.         if (err != fnfErr)
  153.         {
  154.             Error(err);
  155.         }
  156.         return false;
  157.     }
  158.     out->vRefNum = spec->vRefNum;
  159.     out->parID = dirID;
  160.     return true;
  161. }
  162.  
  163.  
  164. static void
  165. Recurse(
  166.     FSSpec *ptr,
  167.     int level)
  168. {
  169.     Report(ptr, level);
  170.     FSSpec newSpec;
  171.     if (IsFolder(ptr))
  172.     {
  173.         for (int ix=0; GetNthItem(ptr, ix+1, &newSpec); ix++)
  174.         {
  175.             Recurse(&newSpec, level+1);
  176.         }
  177.     }
  178.     else
  179.     {
  180.         FixModDate(ptr);
  181.     }
  182. }
  183.  
  184.  
  185. static OSErr
  186. DoOpen(
  187.     AppleEvent *event,
  188.     AppleEvent * /* reply */,
  189.     long /* refCon */)
  190. {
  191.     AEDesc list = { 0, 0 };
  192.     OSErr err = AEGetKeyDesc(event, keyDirectObject, typeAEList, &list);
  193.     if (err)
  194.     {
  195.         Error(err);
  196.         return err;
  197.     }
  198.     long count = 0;
  199.     err = AECountItems(&list, &count);
  200.     if (err)
  201.     {
  202.         Error(err);
  203.         return err;
  204.     }
  205.     for (int ix=0; ix<count; ix++)
  206.     {
  207.         FSSpec topLevel;
  208.         AEKeyword word;
  209.         OSType type;
  210.         Size actualSize;
  211.         err = AEGetNthPtr(&list, ix+1, typeFSS, &word, &type, (Ptr)&topLevel, sizeof(topLevel), &actualSize);
  212.         if (err)
  213.         {
  214.             Error(err);
  215.         }
  216.         else
  217.             Recurse(&topLevel, 0);
  218.     }
  219.     running = false;
  220.     return noErr;
  221. }
  222.  
  223.  
  224. static OSErr
  225. DoQuit(
  226.     AppleEvent * /* event */,
  227.     AppleEvent * /* reply */,
  228.     long /* refCon */)
  229. {
  230.     running = false;
  231.     return noErr;
  232. }
  233.  
  234.  
  235. void
  236. main()
  237. {
  238.     InitToolbox();
  239.     AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(DoOpen), 0, false);
  240.     AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(DoQuit), 0, false);
  241.     GetDateTime(&curModDate);
  242.     while (running)
  243.     {
  244.         EventRecord er;
  245.         er.what = 0;
  246.         WaitNextEvent(-1, &er, 50, NULL);
  247.         SetPort(statusWindow);
  248.         if (er.what == kHighLevelEvent)
  249.         {
  250.             AEProcessAppleEvent(&er);
  251.         }
  252.     }
  253. }
  254.